package com.zelin.token;

import io.jsonwebtoken.*;
import org.junit.Test;

import java.util.Date;

/**
 * Created by WF on 2020/7/27 15:25
 */
public class TestToken {
    //1.创建token的第一种方式
    @Test
    public void testA(){
        //1.1)得到一个jwt的构建器对象
        JwtBuilder jwtBuilder = Jwts.builder().setId("8888")
                .setIssuedAt(new Date())    //设置加密时间
                .setSubject("admin")        //设置主体
                .signWith(SignatureAlgorithm.HS256, "zelin");
        //1.2)得到token
        String token = jwtBuilder.compact();
        //1.3)打印token
        System.out.println("token = " + token);
    }
    //2.解析token
    @Test
    public void testA_1(){
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI4ODg4IiwiaWF0IjoxNTk1ODM0OTcyLCJzdWIiOiJhZG1pbiJ9.sfvG27MvjUjqTaBO5gQoYGgUCXW_djvAHM6JgG4uAi0";
        Claims claims = Jwts.parser().setSigningKey("zelin").parseClaimsJws(token).getBody();
        String id = claims.getId();
        String subject = claims.getSubject();
        Date issuedAt = claims.getIssuedAt();
        System.out.println("id = " + id);
        System.out.println("subject = " + subject);
        System.out.println("issuedAt = " + issuedAt);
    }
    //3.jwt创建（设置加密码时间）
    @Test
    public void testB(){
        JwtBuilder jwtBuilder = Jwts.builder().setId("9999")
                .setSubject("test")
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + 600 * 1000))  //设置过期时间
                .signWith(SignatureAlgorithm.HS256, "zelin");
        String token = jwtBuilder.compact();
        System.out.println("token = " + token);

    }
    //4.jwt解析（带有过期时间）
    @Test
    public void testB_01(){
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI5OTk5Iiwic3ViIjoidGVzdCIsImlhdCI6MTU5NTgzNTQ3NCwiZXhwIjoxNTk1ODM2MDc0fQ.3dfgt33qWFez4Q6u6_qZ-kuTkB1KRFKNgEEnajdqlN8";
        Claims claims = Jwts.parser().setSigningKey("zelin")
                .parseClaimsJws(token).getBody();
        Date issuedAt = claims.getIssuedAt();
        String subject = claims.getSubject();
        Date expiration = claims.getExpiration();
        String id = claims.getId();
        System.out.println("id = " + id);
        System.out.println("issuedAt = " + issuedAt);
        System.out.println("subject = " + subject);
        System.out.println("expiration = " + expiration);
    }
    //5.创建token（携带数据）
    @Test
    public void testC(){
        JwtBuilder jwtBuilder = Jwts.builder().setId("7777")
                .setSubject("aaaa")
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + 300 * 1000))
                .signWith(SignatureAlgorithm.HS256, "zelin")
                .claim("roles", "admin");
        String token = jwtBuilder.compact();
        System.out.println("token = " + token);
    }
    //6.解析token（携带数据）
    @Test
    public void testC_01(){
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI3Nzc3Iiwic3ViIjoiYWFhYSIsImlhdCI6MTU5NTgzNjA1NSwiZXhwIjoxNTk1ODM2MzU1LCJyb2xlcyI6ImFkbWluIn0.b_EkCMRwARvhG6UCkcPVWuzmzf4f_s86v4a1gROs7ac";
        Claims claims = Jwts.parser().setSigningKey("zelin").parseClaimsJws(token).getBody();
        String id = claims.getId();
        Date expiration = claims.getExpiration();
        Date issuedAt = claims.getIssuedAt();
        String subject = claims.getSubject();
        String roles = (String) claims.get("roles");
        System.out.println("id = " + id);
        System.out.println("expiration = " + expiration);
        System.out.println("issuedAt = " + issuedAt);
        System.out.println("subject = " + subject);
        System.out.println("roles = " + roles);
    }
}
